package com.yunhe.rentcars.pro.service.car.impl;

import com.yunhe.rentcars.commons.annoaction.Log;
import com.yunhe.rentcars.commons.constant.Constants;
import com.yunhe.rentcars.commons.dto.Pageable;

import com.yunhe.rentcars.pro.dao.car.CarYearcheckDao;
import com.yunhe.rentcars.pro.dao.car.CarsDao;
import com.yunhe.rentcars.pro.entity.car.CarYearcheck;
import com.yunhe.rentcars.pro.entity.rent.Cars;
import com.yunhe.rentcars.pro.service.car.CarYearcheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author maliang
 * @version V1.0
 * @date 2017/11/25 0025 15:34
 * @Description: 车辆年检业务层
 */
@Service
public class CarYearcheckServiceImpl implements CarYearcheckService {

    @Autowired
    private CarYearcheckDao carYearcheckDao;
    @Autowired
    private CarsDao carsDao;

    @Override
    @Log(value = "添加年检记录",type = 1)
    public void save(CarYearcheck carYearcheck) {
        carstate(carYearcheck);
        carYearcheckDao.save(carYearcheck);
    }

    @Override
    @Log(value = "删除年检记录",type = 2)
    public void delBatch(String ids) {
        String[] split = ids.split(",");
        carYearcheckDao.delBatch(split);
    }

    @Override
    @Log(value = "修改年检记录",type = 3)
    public void update(CarYearcheck carYearcheck) {
        carstate(carYearcheck);
        carYearcheckDao.update(carYearcheck);
    }

    @Override
    public Pageable<CarYearcheck> find(CarYearcheck carYearcheck, Integer offset, Integer limit) {
        Pageable<CarYearcheck> pageable = new Pageable<>();
        Long count = carYearcheckDao.findCount(carYearcheck);
        List<CarYearcheck> rows = null;
        if(count > 0){
            rows = carYearcheckDao.findList(carYearcheck.getCarCode(), carYearcheck.getUsecarTime(), carYearcheck.getReturnTime(), offset, limit);
        }
        pageable.setTotal(count.intValue());
        pageable.setRows(rows);
        return pageable;
    }

    @Override
    public CarYearcheck findOne(Long recordid) {
        return carYearcheckDao.findOne(recordid);
    }

    /**
     * 更改车辆状态
     * @param carYearcheck
     */
    public void carstate(CarYearcheck carYearcheck){
        Date returnTime = carYearcheck.getReturnTime();
        Cars cars = new Cars();
        cars.setCarcode(carYearcheck.getCarCode());
        if(null != returnTime) {
            carYearcheck.setStatus(Constants.CAR_NORMAL);
            cars.setYearcheck(carYearcheck.getUsecarTime());
            cars.setYearcheckend(carYearcheck.getNextTime());
            cars.setCarstate(Constants.CAR_NORMAL);
        } else {
            cars.setCarstate(Constants.CAR_IN_YEARCHECK);
        }
        carsDao.carState(cars);
    }
}
